package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.ExperienceGoldDetailAdapter2;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.ExperienceGoldDeatilData;
import com.kkd.kkdapp.http.entity.ExperienceGoldDetailDataItem;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.Time_Date;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

public class ExperienceGoldDetailActivity extends BaseActivity implements
		OnClickListener, KKDScrollListener {

	private Context mContext = this;

	private static final int SUCCESS_DETAIL_REQUESTEXPERIENCEGOLD = 0;
	private static final int FAIL_DETAIL_REQUESTEXPERIENCEGOLD = 1;

	private RelativeLayout rel_top_back_detail_experienceGold;
	private KkdXlistView mXlistView;
	private ListView mListView;
	private TextView textView_num;
	private ImageView image_null;
	private TextView tv_null;
	private int pageNO = 1;
	private int pageSize = 10;
	private int pageCount;
	private ExperienceGoldDetailAdapter2 adapter;
	private List<ExperienceGoldDetailDataItem> totalList = new ArrayList<ExperienceGoldDetailDataItem>();
	private boolean REFRESHFLAG = true;
	private boolean isShowToastOnRefresh = false;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case SUCCESS_DETAIL_REQUESTEXPERIENCEGOLD:
				if (adapter == null) {
					adapter = new ExperienceGoldDetailAdapter2(mContext,
							totalList);
					mListView.setAdapter(adapter);
				}
				ExperienceGoldDeatilData data = (ExperienceGoldDeatilData) msg.obj;
				String experAmount = "";
				if (data.getExperAmount().contains(".")) {
					experAmount = data.getExperAmount().substring(0,
							data.getExperAmount().indexOf("."));
				} else {
					experAmount = data.getExperAmount();
				}
				textView_num.setText(experAmount);
				int PageSize = data.getPageSize();
				int totalCount = data.getTotalCount();
				if (totalCount % PageSize != 0) {
					if (totalCount == 0) {
						pageCount = 0;
					} else {
						pageCount = (totalCount / PageSize) + 1;
					}
				} else {
					pageCount = (totalCount / PageSize);
				}
				if (pageNO > pageCount) {
					mXlistView.stopLoadMore(-1);
					if (REFRESHFLAG) {
						mXlistView.stopLoadMore(0);
					}
				} else {
					mXlistView.setNoMore(false);
					if (pageNO == 1) {
						totalList.clear();
					}
					totalList.addAll(data.getDataList());
					adapter.notifyDataSetChanged();
					if (data.getDataList().size() < pageSize) {
						mXlistView.stopLoadMore(1);
					} else {
						mXlistView.stopLoadMore(0);
					}
				}
				mXlistView.stopRefresh();
				Time_Date time_Date = new Time_Date();
				String time = time_Date.get_Year_Day();
				mXlistView.setRefreshTime(time);
				if (REFRESHFLAG) {
					if (totalList == null || totalList.size() == 0) {
						image_null.setImageResource(R.drawable.img_null);
						tv_null.setText("暂无明细");
						image_null.setVisibility(View.VISIBLE);
						tv_null.setVisibility(View.VISIBLE);
					} else {
						image_null.setVisibility(View.GONE);
						tv_null.setVisibility(View.GONE);
					}
				}

				if (!isShowToastOnRefresh) {
					isShowToastOnRefresh = true;
				}
				break;

			case FAIL_DETAIL_REQUESTEXPERIENCEGOLD:
				mXlistView.stopLoadMore(1);
				mXlistView.stopRefresh();

				if (totalList == null || totalList.size() == 0) {
					image_null.setImageResource(R.drawable.img_null_net);
					if (msg.arg1 == KkdHttpClient.NO_NET) {
						tv_null.setText("网络错误，请稍候下拉刷新重试");
					} else {
						tv_null.setText("服务异常，请稍候下拉刷新重试");
					}
					image_null.setVisibility(View.VISIBLE);
					tv_null.setVisibility(View.VISIBLE);
				}

				if (isShowToastOnRefresh) {
					String desc = (String) msg.obj;
					if (!TextUtils.isEmpty(desc)) {
						AppUtil.makeToastForCustomer(mContext, desc);
					}
				} else {
					isShowToastOnRefresh = true;
				}
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_experience_gold_detail);
		initView();
		initListener();
		initData();
	}

	private void initView() {
		rel_top_back_detail_experienceGold = (RelativeLayout) findViewById(R.id.rel_top_back_detail_experienceGold);
		image_null = (ImageView) findViewById(R.id.id_no_content);
		tv_null = (TextView) findViewById(R.id.id_no_content_text);
		mXlistView = (KkdXlistView) findViewById(R.id.kkdXlistView_detail_experienceGold);
		mListView = mXlistView.mListView;
		mXlistView.setKKDScrollListener(this);
		mListView.setPadding(0, 0, 0, 0);
		mListView.setDividerHeight(0);
		/*
		 * 初始化KkdXlistView
		 */
		/*
		 * headView
		 */
		// head
		RelativeLayout headViewContainer = new RelativeLayout(mContext);
		headViewContainer.setGravity(Gravity.CENTER_VERTICAL);
		headViewContainer
				.setLayoutParams(new android.widget.AbsListView.LayoutParams(
						android.widget.AbsListView.LayoutParams.MATCH_PARENT,
						android.widget.AbsListView.LayoutParams.WRAP_CONTENT));
		headViewContainer.setPadding(0, 0, 0, SystemUtils.dip2px(mContext, 15));
		headViewContainer.setBackgroundColor(Color.parseColor("#F0EEF3"));
		RelativeLayout headView = new RelativeLayout(mContext);
		headView.setGravity(Gravity.CENTER_VERTICAL);
		headView.setLayoutParams(new android.widget.AbsListView.LayoutParams(
				android.widget.AbsListView.LayoutParams.MATCH_PARENT,
				android.widget.AbsListView.LayoutParams.WRAP_CONTENT));
		headView.setPadding(SystemUtils.dip2px(mContext, 15),
				SystemUtils.dip2px(mContext, 10),
				SystemUtils.dip2px(mContext, 15),
				SystemUtils.dip2px(mContext, 10));
		headView.setBackgroundColor(Color.WHITE);
		// *******************textview可用特权本金************************
		TextView textView_title = new TextView(mContext);
		LayoutParams param0 = new android.widget.RelativeLayout.LayoutParams(
				android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
				android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
		((android.widget.RelativeLayout.LayoutParams) param0)
				.addRule(RelativeLayout.CENTER_VERTICAL);
		textView_title.setLayoutParams(param0);
		textView_title.setText("可用特权本金(元)");
		textView_title.setTextColor(Color.parseColor("#A9A9A9"));
		headView.addView(textView_title);
		// *******************textview可用特权本金金额************************
		textView_num = new TextView(mContext);
		android.widget.RelativeLayout.LayoutParams params = new android.widget.RelativeLayout.LayoutParams(
				android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
				android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		textView_num.setLayoutParams(params);
		textView_num.setTextSize(sp(8));
		textView_num.setTextColor(Color.parseColor("#FF7301"));
		headView.addView(textView_num);
		headViewContainer.addView(headView);
		// listview
		mListView.addHeaderView(headViewContainer);

		mXlistView.startRefresh();
	}

	private void initListener() {
		rel_top_back_detail_experienceGold.setOnClickListener(this);
	}

	/*
	 * 请求特权本金明细接口
	 */
	private void requsetDetail() {
		String customerIdAndToken = PreferenceUtils.getInstance()
				.getcustomerId(mContext);
		String[] cat = customerIdAndToken.split("&");
		if (cat == null || cat.length < 2)
			return;
		KkdHttpMagager.getInstance().ExperienceColdDetail(pageNO, pageSize,
				ExperienceGoldDeatilData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						ExperienceGoldDeatilData data = (ExperienceGoldDeatilData) responseBody;
						Message msg = Message.obtain();
						msg.what = SUCCESS_DETAIL_REQUESTEXPERIENCEGOLD;
						msg.obj = data;
						mHandler.sendMessage(msg);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = Message.obtain();
						msg.what = FAIL_DETAIL_REQUESTEXPERIENCEGOLD;
						msg.obj = desc;
						msg.arg1 = statusCode;
						mHandler.sendMessage(msg);
					}
				});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rel_top_back_detail_experienceGold:
			finish();
			break;

		default:
			break;
		}
	}

	private void initData() {
		requsetDetail();
		// if (AppUtil.isConnect(mContext)) {
		// requsetDetail();
		// } else {
		// if (totalList == null || totalList.size() == 0) {
		// image_null.setVisibility(View.VISIBLE);
		// tv_null.setVisibility(View.VISIBLE);
		// tv_null.setText("网络连接失败，请检查网络！");
		// image_null.setImageResource(R.drawable.img_null_net);
		// mXlistView.stopRefresh();
		// } else {
		// showToast("网络连接失败");
		// mXlistView.stopLoadMore(0);
		// mXlistView.stopRefresh();
		// }
		// }
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		REFRESHFLAG = true;
		initData();
	}

	@Override
	public void onLoadMore() {
		if (AppUtil.isConnect(mContext)) {
			pageNO++;
			REFRESHFLAG = false;
		}
		initData();
	}

	private float sp(final int sp) {
		float scaledDensity = getResources().getDisplayMetrics().scaledDensity;
		return sp * scaledDensity;
	}
}
